#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "wbio.h"

#include "nvtfat.h"
#include "ide.h"

INT  InitHardDisk(INT nEngineClock);

static INT  HD_init(PDISK_T *ptPDisk)
{

	return 0;
}

static INT  HD_ioctl(PDISK_T *ptPDisk, INT control, VOID *param)
{
	return 0;
}

static INT  HD_read(PDISK_T *ptPDisk, UINT32 uSecNo, 
								INT nSecCnt, UINT8 *pucBuff)
{
    if (!atapiRead(0,(UINT32)pucBuff, uSecNo, nSecCnt))		
   		printf("read error\n");
       
	
	return FS_OK;
}


static INT  HD_write(PDISK_T *ptPDisk, UINT32 uSecNo, 
								INT nSecCnt, UINT8 *pucBuff, BOOL bWait)								
{
    if (!atapiWrite(0,(UINT32)pucBuff, uSecNo, nSecCnt))
   		printf("write error\n");
	
	return FS_OK;
}


STORAGE_DRIVER_T  _HDDriver = 
{
	HD_init,
	HD_read,
	HD_write,
	HD_ioctl,
};


INT  InitHardDisk(INT nEngineClock)
{
	PDISK_T		*ptPDisk;
	UINT32  sector;	
	
	if(atapiInit(ATAPI_CLK_66MHZ))
    {        	
        atapiIoctl(ATAPI_MASTER, ATAPI_GET_CAPACITY, (UINT32)&sector);
        printf("\nMaster HD Total sectors : %d\n",sector);
        printf("Capacity : %2.2fG\n",sector/2000000.);
    }
    else
	{
	    printf("HD not found\n");
	    return 0;
    }

    
    if(!atapiOpen())
     	return 0;
     	
	ptPDisk = malloc(sizeof(PDISK_T));
	ptPDisk->nDiskType = DISK_TYPE_HARD_DISK | DISK_TYPE_DMA_MODE;
	ptPDisk->uTotalSectorN= sector;
	ptPDisk->uDiskSize = (ptPDisk->uTotalSectorN/1000)<<9;
	ptPDisk->ptDriver = &_HDDriver;
	fsPhysicalDiskConnected(ptPDisk);

	return 1;
}

